package com.hyst.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/***
 * @Des: Cookie
 * @Author: jiyang
 * @Date: 2018-10-26 14:36
 */
public class CookieUtil {

	/**
	 * 设置cookie有效期，根据需要自定义[本系统设置为30天]
	 */
	public final static int COOKIE_MAX_AGE = 30 * 24 * 3600;

	/**
	 * @param response
	 * @param name
	 * @desc 删除指定Cookie
	 */
	public static void removeCookie(HttpServletResponse response, String name) {
		Cookie cookie = new Cookie(name, null);
		cookie.setPath("/");
		cookie.setValue("");
		cookie.setMaxAge(-1);
		response.addCookie(cookie);
	}

	/**
	 * @param request
	 * @param name
	 * @return
	 * @desc 根据Cookie名称得到Cookie的值，没有返回Null
	 */
	public static String getCookieValue(HttpServletRequest request, String name) {
		Cookie cookie = getCookie(request, name);
		if (cookie != null) {
			try {
				return URLDecoder.decode(cookie.getValue(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * @param request
	 * @param name
	 * @desc 根据Cookie名称得到Cookie对象，不存在该对象则返回Null
	 */
	public static Cookie getCookie(HttpServletRequest request, String name) {
		Cookie cookies[] = request.getCookies();
		if (cookies == null || name == null || name.length() == 0)
			return null;
		Cookie cookie = null;
		for (int i = 0; i < cookies.length; i++) {
			if (!cookies[i].getName().equals(name))
				continue;
			cookie = cookies[i];
			if (request.getServerName().equals(cookie.getDomain()))
				break;
		}

		return cookie;
	}

	/**
	 * @param response
	 * @param name
	 * @param value
	 * @desc 添加一条新的Cookie信息，默认有效时间为一个月
	 */
	public static void setCookie(HttpServletResponse response, String name, String value) {
		setCookie(response, name, value, COOKIE_MAX_AGE);
	}

	/**
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAge
	 * @desc 添加一条新的Cookie信息，可以设置其最长有效时间(单位：秒)
	 */
	public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
		if (value == null)
			value = "";
		Cookie cookie = new Cookie(name, value);
		if (maxAge != 0) {
			cookie.setMaxAge(maxAge);
		} else {
			cookie.setMaxAge(COOKIE_MAX_AGE);
		}
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	/**
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAge
	 * @desc 添加一条新的Cookie信息，可以设置其最长有效时间(单位：秒)
	 */
	public static void setCookie(HttpServletResponse response, String name, String value, int maxAge, String domain) {
		if (value == null)
			value = "";
		Cookie cookie = new Cookie(name, value);
		if (maxAge != 0) {
			cookie.setMaxAge(maxAge);
		} else {
			cookie.setMaxAge(COOKIE_MAX_AGE);
		}
		cookie.setPath("/");
		if (!StringUtils.isBlank(domain)) {
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);
	}
}